# A simple flight-runner, dodge the walls!

: wall
#	0b11111111
	0b11110000

: ship
	0b11110000
	0b00011000
	0b11110000

: line
	0b11111111

: oops
	0x44 0xAA 0xAA 0xAA 0x44
	0xCD 0xA9 0xC5 0x84 0x8D

# variables!
:alias shipx ve
:alias shipy vd
:alias wallx vc
:alias topwall vb
:alias bottomwall va
:alias prevtopwall v9
:alias prevbottomwall v8
:alias wallcounter v7


# non permanent variables
:alias newx v3
:alias newy v4

: setup
	wallx := 64
	topwall := 3
	bottomwall := 28
	prevtopwall := topwall 
	prevbottomwall := bottomwall 
	shipx := 4
	shipy := 14
	v0 := 0
	v1 := topwall 
	v2 := bottomwall 
	i := line
	loop
		sprite v0 v1 1
		sprite v0 v2 1
		v0 += 8
		if v0 != 64 then
	again
	i := ship
	sprite shipx shipy 3
;

: update-player
	newx := shipx
	newy := shipy
	v2 := 1
	v1 := 0

	# up
	v0 := 5
	if v0 key then 
		newy -= v2
	# down
	v0 := 8
	if v0 key then 
		newy += v2
	# left
	v0 := 7
	if v0 key then 
		newx -= v2
	# right
	v0 := 9
	if v0 key then 
		newx += v2

	# check ship bounds
	if newy == 255 then
		newy := 0
	if newy == 30 then
		newy := 29
	if newx == 255 then
		newx := 0
	if newx == 58 then
		newx := 57


	# verify if we changed positions
	if newx != shipx then 
		v1 := v2
	if newy != shipy then 
		v1 := v2
	# if not, just return
	if v1 != v2 then 
		return
	# Otherwise, let's update
	i := ship
	sprite shipx shipy 3
	shipx := newx 
	shipy := newy 
	sprite shipx shipy 3
;

: update-walls
#	wallx := 63
#	topwall := 1
#	bottomwall := 30
#	wallcounter := 14
#	prevtopwall := 1
#	prevbottomwall := 30

	i := wall
	# wall draw rate
	v0 := 4
	v1 := 0
	# Move one to the left, draw the new wall
	# undraw the old one, which if they're at the same
	# place, just redraws the wall
	wallx -= v0
	sprite wallx topwall 1
	v1 += vf
	sprite wallx bottomwall 1
	v1 += vf
	sprite wallx prevtopwall 1
	v1 += vf
	sprite wallx prevbottomwall 1
	v1 += vf

	# We drew on the ship
	if v1 == 3 then
		game-over

	# If we're at the very left, swap past the right edge
	# when we return, it will decrement to in the right edge
	if wallx == 0 then
		wallx := 64
	if wallx != 64 then
		return

	# Randomly decide if we're moving walls
	v0 := random 0b00000011
	v1 := 1
	prevtopwall := topwall
	prevbottomwall := bottomwall
	if v0 == 0 then 
		topwall += v1
	if v0 == 1 then 
		bottomwall -= v1
	if v0 == 2 then 
		topwall += v1
	if v0 == 3 then 
		bottomwall -= v1

	v2 := bottomwall
	v2 -= topwall
	# if we're not squished
	if v2 != 5 then
		return

	# becomes 1 if topwall moved down
	v0 := topwall
	v0 -= prevtopwall
		
	# becomes 1 if topwall moved up
	v1 := prevbottomwall
	v1 -= bottomwall


		

	if v0 == 1 then
		bottomwall += v0
	if v1 == 1 then
		topwall -= v1

	# bounds checking
	v2 := 1
	if topwall == 1 then
		bottomwall += v2 
	if bottomwall == 31 then
		topwall -= v2

;

: game-over
	i := oops
	v0 := 5
	v1 := 14
	if shipx <= 30 then
		v0 := 42
	sprite v0 v1 5
	v2 := 5
	i += v2
	v0 += 8
	sprite v0 v1 5
	v0 := 140
	delay := v0
	loop
		v0 := delay
		if v0 != 0 then
	again
	clear
	main
;

: check-collision
	if vf == 1 then
		game-over
;

# shamelessly stolen from docs!
: sync
    loop
        v0 := delay
        if v0 != 0 then
    again

    # delay for up to 1/60th of a second
    # using the fixed-rate delay timer
    v0 := 2
    delay := v0
;

: main
	setup
	loop
		update-player
		check-collision
		sync
		update-walls
		vf := 0
	again